// 导出公用函数变量等，并不是vue2的mixins
import listApis from '@/apis/list.js';
export function useSubjectQuery() {
	const listItems = ref([]);
	const exams = ref([]);
	const subjects = ref([]);


	function query() {
		return listApis.industryList().then((res) => {
			listItems.value = res.data.map((e) => {
				return {
					id: e.id,
					subjectName: e.name,
					code: e.code,
					shortName: e.name
				};
			});
			// listItems.value.unshift({
			// 	id: 0,
			// 	subjectName: '全部',
			// 	code: '-1',
			// 	shortName: ''
			// });
		});
	}
	function getExams(industryId) {
		return listApis
			.examByIndustry({
				params: {
					size: 1000,
					industryId: industryId
				}
			})
			.then((res) => {
				exams.value = res.data.map((e) => {
					return {
						...e,
						id: e.id,
						subjectName: e.name,
						code: e.code,
						shortName: e.name
					};
				});
			});
	}
	function getSubjects(examId, parentId) {
		return listApis.leafList({
			params: {
				examId,
				parentId
			}
		});

		// .then(res=>{
		// 	subjects.value=res.data.map(e => {
		// 		return {
		// 			...e,
		// 		  id: e.id,
		// 		  subjectName: e.name,
		// 		  code: e.code,
		// 		  shortName: e.name
		// 		}
		// 	  })
		// })
	}
	// 返回二维数组,不会有空数组（除非没有任何科目）,格式:[]或者[[1]],不会有[[]]
	async function getSubjectsRecursion(examId, parentId = 0, res = []) {
		const result = await listApis.leafList({
			params: {
				examId,
				parentId
			}
		});
		if (result.data && result.data.length > 0) {
			res.push(
				result.data.map((e) => {
					return {
						...e,
						id: e.id,
						subjectName: e.name,
						code: e.code,
						shortName: e.name
					};
				})
			);

			if (result.data[0].hasChild) {
				// 递归,返回promise
				return await getSubjectsRecursion(examId, result.data[0].id, res);
			}
			return res;
		}

		return res;
	}
	return {
		listItems,
		exams,
		subjects,
	
		getExams,
		query,
		getSubjects,
		getSubjectsRecursion
	};
}
